var express = require('express');
var router = express.Router();
var https = require('https');

//解决express跨域问题
router.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header('Access-Control-Allow-Headers', 'Content-Type');
    next();
});

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

//天气预报
router.get('/weather', function(req, res) {
  var lat = req.query.latitude
  var lot = req.query.longitude
  
  https.get('https://mainsite-restapi.ele.me/bgs/weather/current?latitude='+ lat +'&longitude='+ lot ,function(response){
    var data =''
    response.on('data',function(chunk){
      data += chunk
    })
    
    response.on('end',function(){
      res.send(data)
    })
  } )
});

//搜索定位
router.get('/address', function(req, res) {
  var keyword = req.query.keyword
  var lat = req.query.latitude
  var lot = req.query.longitude
  
  https.get('https://mainsite-restapi.ele.me/bgs/poi/search_poi_nearby?keyword='+ keyword +'&offset=0&limit=20&longitude='+ lot+'&latitude=' + lat,function(response){
    var data =''
    response.on('data',function(chunk){
      data += chunk
    })
    
    response.on('end',function(){
      res.send(data)
    })
  } )
});

router.get('/hot_search', function(req, res) {  //热搜
  var lat = req.query.latitude
  var lot = req.query.longitude
  var geohash = req.query.geohash
  var url

  if(geohash){  //判断是首页的热搜还是搜索页的热搜（详情页有这个参数）
    url = 'https://mainsite-restapi.ele.me/shopping/v3/hot_search_words?latitude=' + lat + '&longitude=' + lot +'&geohash=' +geohash
  }else{
    url = 'https://mainsite-restapi.ele.me/shopping/v3/hot_search_words?latitude=' + lat + '&longitude=' + lot
  }

  https.get(url, function(response){
    var data =''
    response.on('data',function(chunk){
      data += chunk
    })
    
    response.on('end',function(){
      res.send(data)
    })
  })
});

router.get('/nearby', function(req, res) {  //附近美食
  var lat = req.query.latitude
  var lot = req.query.longitude
  
  https.get('https://mainsite-restapi.ele.me/shopping/v2/entries?latitude=' + lat + '&longitude=' + lot
  + '&templates[]=main_template', function(response){
    var data =''
    response.on('data',function(chunk){
      data += chunk
    })
    
    response.on('end',function(){
      res.send(data)
    })
  })
});

router.get('/seller', function(req, res) {  //商家
  var lat = req.query.latitude
  var lot = req.query.longitude
  var limit = req.query.limit

  https.get('https://mainsite-restapi.ele.me/shopping/restaurants?latitude=' + lat + '&longitude=' + lot
  + '&offset=20&limit=' + limit + '&extras[]=activities&terminal=h5', function(response){
    var data =''
    response.on('data',function(chunk){
      data += chunk
    })
    
    response.on('end',function(){
      res.send(data)
    })
  })
});

router.get('/discovery/platform', function(req, res) {  //发现页平台
  var lat = req.query.latitude
  var lot = req.query.longitude

  https.get('https://mainsite-restapi.ele.me/member/v1/discover?platform=1&block_index=0&latitude='
  + lat + '&longitude=' + lot, function(response){
    var data =''
    response.on('data',function(chunk){
      data += chunk
    })
    
    response.on('end',function(){
      res.send(data)
    })
  })
});


router.get('/discovery/hotfood', function(req, res) {  //发现美食热销
  var lat = req.query.latitude
  var lot = req.query.longitude

  https.get('https://mainsite-restapi.ele.me/hotfood/v1/guess/likes?latitude=' + lat + '&longitude=' + lot
  + '&offset=0&limit=3&request_id=ba91be43-aa41-498c-93f7-4811cde772d6&tag_id=-1&columns=1', function(response){
    var data =''
    response.on('data',function(chunk){
      data += chunk
    })
    
    response.on('end',function(){
      res.send(data)
    })
  })
});

// http://localhost:3000/getMenuData   获取菜单栏数据--头部
router.get('/getMenuData', function(req, res, next) {
  var lat = req.query.latitude
  var lot = req.query.longitude
	var id = req.query.id;
	// https://mainsite-restapi.ele.me/shopping/restaurant/1383166?extras[]=activities&extras[]=albums&extras[]=license&extras[]=identification&latitude=22.58846&longitude=113.97459
	var url = 'https://mainsite-restapi.ele.me/shopping/restaurant/'+id+'?extras[]=activities&extras[]=albums&extras[]=license&extras[]=identification&latitude='+lat+'&longitude='+lot;
	// console.log(id);
  	https.get(url,function(response){
  		var html = "";
  		response.on("data",function(chunk){
  			html+=chunk;
  		})

  		response.on("end",function(){
  			res.send(html);
  		})
  	})
});


// http://localhost:3000/getMenuData   获取菜单栏数据--列表
router.get('/getMenuList', function(req, res, next) {
	var id = req.query.id;
	var url = 'https://mainsite-restapi.ele.me/shopping/v2/menu?restaurant_id='+id;
	// console.log(id);
  	https.get(url,function(response){
  		var html = "";
  		response.on("data",function(chunk){
  			html+=chunk;
  		})

  		response.on("end",function(){
  			res.send(html);
  		})
  	})
});

// http://localhost:3000/getScore   获取评价列表头部星数=
router.get('/getScore', function(req, res, next) {
	var id = req.query.id;
	var url = 'https://mainsite-restapi.ele.me/ugc/v2/restaurants/'+id+'/ratings/scores';
	// console.log(id);
  	https.get(url,function(response){
  		var html = "";
  		response.on("data",function(chunk){
  			html+=chunk;
  		})

  		response.on("end",function(){
  			res.send(html);
  		})
  	})
});
// http://localhost:3000/getRating   获取评价列表--满意等级
router.get('/getRating', function(req, res, next) {
	var id = req.query.id;
	var url = 'https://mainsite-restapi.ele.me/ugc/v2/restaurants/'+id+'/ratings/tags';
	// console.log(id);
  	https.get(url,function(response){
  		var html = "";
  		response.on("data",function(chunk){
  			html+=chunk;
  		})

  		response.on("end",function(){
  			res.send(html);
  		})
  	})
});
// http://localhost:3000/getPingJiaList    获取评价列表--用户评价列表
router.get('/getPingJiaList', function(req, res, next) {
	var id = req.query.id;
	var url = 'https://mainsite-restapi.ele.me/ugc/v2/restaurants/'+id+'/ratings?has_content=true&offset=0&limit=10';
	// console.log(id);
  	https.get(url,function(response){
  		var html = "";
  		response.on("data",function(chunk){
  			html+=chunk;
  		})

  		response.on("end",function(){
  			res.send(html);
  		})
  	})
});

module.exports = router;
